<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3" xmlns:head="http://www.w3.org/1999/html">
<head th:include="public/public_css::public_css ('订单驱动补货')" />

<body style="background-color: #f2f2f2;">
<div class="layui-fluid larry-wrapper">
    <div class="layui-card">
        <div class="layui-row layui-col-space30">
            <div class="layui-col-xs24">
                <form class="layui-form layui-card-header  layui-card-header-auto" id="searchForm">
                    <div class="layui-inline">
                        <label class="layui-form-label">创建时间</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" id="cjsj" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">状态</label>
                        <select name="zt" lay-filter="aihao">
                            <option value="">全部</option>
                            <option value="0">已生成</option>
                            <option value="1">已确认</option>
                            <option value="2">作废</option>
                        </select>
                    </div>
                    <div class="layui-inline inline-search-btn">
                        <a class="layui-btn layui-btn-normal" data-type="reload" id="search_button"> 搜索 </a>
                    </div>
                    <div class="layui-inline inline-reset-btn">
                        <button type="reset" class="layui-btn layui-btn-primary" onclick="queryTable()" style="background-color: #40AFFE">重置</button>
                    </div>
                    <input type="hidden" id="cjsj_start" name="cjsj_start">
                    <input type="hidden" id="cjsj_end" name="cjsj_end">
                </form>
                <!--顶部按钮展示区域 【 tpl-button 】 -->
                <div class="group-button tpl-button" id="toolbar"></div>
                <!--列表-->
                <section class="panel panel-padding layui-card-body" >
                    <div class="pro">
                        <table id="table" class="table text-nowrap"></table>
                    </div>
                </section>
            </div>
        </div>
    </div>
</div>
</body>


<div th:include="public/public_js::public_js"></div>
<div th:include="public/public_btn::public_btn"></div>
<style type="text/css">

</style>
<script th:inline="javascript">
    var functions = [[${functions}]];
    //	回车查询
    document.onkeydown = function(event) {
        e = event ? event : (window.event ? window.event : null);
        if (e.keyCode == 13) {
            //执行的方法
            $('#search_button').click();
        }
    };
    $(function () {
        $(".moreBtn").on("click",function () {
            $(".moreSearch").toggle();
        });
    });
    layui.use(['list','layer','zzpUtils','jquery','laydate', 'tablebutton'],function () {
        var zzpUtils = layui.zzpUtils,
            $ = layui.jquery,
            laydate = layui.laydate;
        tablebtn = layui.tablebutton;
        tablebtn.buildOnClick({
            type: 1,
            temp: $("#function_tpl"),
            functions: functions.headBtn,
            row: null,
            event: {
                searchEvent: function () {
                    zzpUtils.refreshTable("#table");
                },
                // 行按钮查看
                getEvent: function (data) {
                    cha_kuwei(data);
                },
                // 新增按钮
                addEvent: function (data) {
                    dialogAdd(data);
                },
                //  删除
                deleteEvent: function (data) {
                    manyDelete(data)
                },
                // 导出
                exportEvent: function (data) {
                    manyExport(data);
                },
                // 查看补货任务
                searchBhEvent: function (data) {
                    search_Warrant(data);
                },
                // 下发补货任务
                xiafaRwEvent: function (data) {
                    Issued_Warrant(data);
                }
            }
        });
//        window.operateEvent = {
//            "click #searchRow": function (e, value, row, index) {//行按钮 查看
//                cha_kuwei(row.id);
//            },
//            "click #searchWarrant": function (e, value, row, index) {//行 查看补货任务  btn_stop
//                search_Warrant(row.id);
//            },
//            "click #IssuedWarrant": function (e, value, row, index) {//行 下发补货任务  btn_stop
//                Issued_Warrant(row.id);
//            },
//            "click #deleteWarrant": function (e, value, row, index) {//行  删除  btn_stop
//                console.log(row.id);
//                var ids = [];
//                ids.push(row.id);
//                $.ajax({
//                    url: "/zxsswms/wmsorderrepair/delete",
//                    type: "POST",
//                    dataType: "JSON",
//                    contentType: 'application/json',
//                    async: false,
//                    data:JSON.stringify({ids}),
//                    success: function (data) {
//                        console.log(666666, data);
//                        if (data.type) {
//                            layer.msg(data.message);
//                        }
//                    },
//                    error: function () { console.log("Error"); }
//                });
//            }
//        }
        var cjsj_start;
        var cjsj_end;
        laydate.render({
            elem: '#cjsj',
            type: 'date',
            range: '~',
            done: function (value, date, endDate) {
                if (value != null && value != "") {
                    cjsj_start = value.toString().substring(0, value.toString().indexOf("~"));
                    cjsj_end = value.toString().substring(value.toString().indexOf("~") + 1);
                    $("#cjsj_start").val(Date.parse(new Date(cjsj_start)));
                    $("#cjsj_end").val(Date.parse(new Date(cjsj_end)));
                } else {
                    $("#cjsj_start").val("");
                    $("#cjsj_end").val("");
                }
            }
        });
        laydate.render({
            elem: '#test5'
            , theme: '#20a0ff'
        })
        laydate.render({
            elem: '#test6'
            , theme: '#20a0ff'
        })
        var $table = zzpUtils.table({
            elem: '#table',
            isApi: true,
            isOperate: true,
            pagination: true,
            url: '/wmsorderrepair/list',
            columns: [
                {
                    field: '',
                    checkbox: true
                }, {
                    field: 'bhbh',
                    title: '补货编号'
                }, {
                    field: 'cjsj',
                    title: '创建时间'
                }, {
                    field: 'qrsj',
                    title: '确认时间'
                }, {
                    field: 'cjry',
                    title: '创建人员'
                }, {
                    field: 'show_zt',
                    title: '补货单状态'
                }, {
                    field: 'show_sczt',
                    title: '生成状态'
                }, {
                    field: 'qrry',
                    title: '确认人员'
                }],
            queryParams: function () {//查询条件
                return zzpUtils.getFormJson($("#searchForm"));
            },
            detailFormatter: function (index, row) {
                return "这里就没有确定过11";
            },
            /*定义表格右侧按钮*/
            operateFormatter: operateFormatter
        });

        function operateFormatter(value, row, index) {
            console.log('row.id', row.id);
            var button1 = '', buttons2 = '', buttons3 = '';
            var button = '';
            for (var i = 0; i < functions.tableBtn.length; i++) {
                var item = functions.tableBtn[i];
                console.log('按钮按钮', item);
                var btn = '<button class="layui-btn layui-btn-xs layui-btn-normal" lay-event="' + functions.tableBtn[i].event + '">'+ functions.tableBtn[i].name + '</button>';

//                var btn = '<a class="layui-btn layui-btn-sm layui-btn-warm" lay-event="' + functions.tableBtn[i].event + '">'
//                    + functions.tableBtn[i].name
//                    + '</a>';
                if (row.zt == '2' && row.sczt== '0') {
                    if (functions.tableBtn[i].name == '查看') {
                        button += btn;
                    }
                } else if(row.zt !== '2'){
                    if (functions.tableBtn[i].name == '查看补货任务' ||functions.tableBtn[i].name == '查看') {
                        button += btn;
                    }
                }
                else {
                        button += btn;
                }
            }
            button += '</span>'
                + '</div>';
            return button;
        }
        $(".inline-search-btn").on('click', function () {
            zzpUtils.refreshTable("#table");
        });
        // 多行 删除
        function manyDelete(data) {
            var ids = [];
            //使用getSelections即可获得行数据，row是json格式的数据
            var getSelectRows = $("#table").bootstrapTable('getSelections', function (row) {
                return row;
            });
//        console.log('getSelectRows',getSelectRows);
            getSelectRows.forEach(item => {
                console.log(item);
                ids.push(item.id);
            });
            console.log('getSelectRows', ids);
            if (ids.length < 1) {
                layer.msg("请至少选择一条删除");
            } else {
                $.ajax({
                    url: "/zxsswms/wmsorderrepair/delete",
                    type: "POST",
                    dataType: "JSON",
                    contentType: 'application/json',
                    async: false,
                    data: JSON.stringify({ids}),
                    success: function (data) {
                        console.log(666666, data);
                        if (data.type === "error") {
                            layer.msg(data.message);
                        }
//                    $table.bootstrapTable('append', $('#bhsl').val());
                    },
                    error: function () {
                        console.log("Error");
                    }
                });
            }
        }

        /*顶部按钮导出 */
        function manyExport(data) {
            var arr = [];
            var $rows = $table.bootstrapTable('getSelections');
            if ($rows.length > 0) {
                arr = $.map($rows, function (row) {
                    return row.id;
                })
            }
            /*
             * 以下是将ids和对象拼接在一起
             * */
            var obj = [];
            for (var i = 0; i < arr.length; i++) {
                var person = {};
                person[i] = arr[i];
                obj.push(person);
            }
            var str = zzpUtils.getFormJson($("#searchForm"));
            console.log('arr', arr);
            var object2 = Object.assign({ids: arr}, str);
            /*将条件查询和ids传到后台*/
            zzpUtils.outputXslx({
                url: "/wmsorderrepair/getOutPutList",
                data: JSON.stringify(object2)
            });//导出结束
        }

        /*查看*/
        function cha_kuwei(data) {
            console.log('idid', data);
            var id = data.row.id;
            var zzpUtils = layui.zzpUtils,
                $ = layui.jquery,
                laydate = layui.laydate;
            zzpUtils.dialog({
                title: "查看补货单",
                type: 2,
                content: "/stock/toWarrant?id=" + id,
                area: ['70%', '70%'], //设置dialog大小，也可以不设置，
            });
        }

        //新增补货单
        function dialogAdd(data) {
            var zzpUtils = layui.zzpUtils,
                $ = layui.jquery,
                laydate = layui.laydate;
            zzpUtils.dialog({
                title: "新增补货单",
                type: 2,
                content: "/stock/toAddWarrant",
//            area: ['55%', '55%'], //设置dialog大小，也可以不设置，
            })
        };
        // 查看补货任务
        function search_Warrant(data) {
            var id = data.row.id;
            var zzpUtils = layui.zzpUtils,
                $ = layui.jquery,
                laydate = layui.laydate;
            zzpUtils.dialog({
                title: "查看补货任务",
                type: 2,
                content: "/stock/toSearchWarrant?id=" + id,
//            area: ['55%', '55%'], //设置dialog大小，也可以不设置，
            });
        };
        // 下发补货任务
        function Issued_Warrant(data) {
            var id = data.row.id;
            var zzpUtils = layui.zzpUtils,
                $ = layui.jquery,
                laydate = layui.laydate;
            zzpUtils.dialog({
                title: "下发补货任务",
                type: 2,
                content: "/stock/toIssueWarrant?id=" + id,
                area: ['40%', '40%'], //设置dialog大小，也可以不设置，
            });
        }
        // 重置
        window.queryTable = function () {
            location.reload();
        }
    })
</script>
</html>